package com.itchen.common;

import com.itchen.exception.code.BaseResponseCode;
import com.itchen.exception.code.ResponseCodeInterface;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

@Data
@Accessors(chain = true)
public class DataResult<T> {
    /**
     * 响应状态码
     */
    private int code;

    /**
     * 响应提示语
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 封装响应数据
     */
    private Map<String, Object> dataMap = new HashMap<>();

    public DataResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public DataResult(int code, T data) {
        this.code = code;
        this.data = data;
        this.msg=null;
    }

    public DataResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public DataResult() {
        this.code= BaseResponseCode.SUCCESS.getCode();
        this.msg=BaseResponseCode.SUCCESS.getMsg();
        this.data=null;
    }

    public DataResult(String msg,int code) {
        this.code= code;
        this.msg=msg;
        this.data=null;
    }

    public DataResult(T data) {
        this.data = data;
        this.code= BaseResponseCode.SUCCESS.getCode();
        this.msg=BaseResponseCode.SUCCESS.getMsg();
    }

    public DataResult(String msg) {
        this.data = null;
        this.code= 500;
        this.msg=BaseResponseCode.SUCCESS.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface) {
        this.data = null;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface, T data) {
        this.data = data;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }
    /**
     * 操作成功 data为null
     */
    public static DataResult success(){
        return new DataResult();
    }

    /**
     * 操作成功 data为null,可自定义信息
     */
    public static DataResult success(String msg){
        return new DataResult(msg,200);
    }

    /**
     * 操作成功 data 不为null
     */
    public static <T>DataResult success(T data){
        return new DataResult(data);
    }

    /**
     * 操作失败
     */
    public static <String>DataResult error(String msg){
        return new DataResult(msg);
    }

    /**
     * 自定义 返回操作 data 可控
     */
    public static <T>DataResult getResult(int code,String msg,T data){
        return new DataResult(code,msg,data);
    }

    /**
     *  自定义返回  data为null
     */
    public static DataResult getResult(int code,String msg){
        return new DataResult(code,msg);
    }

    /**
     * 自定义返回 入参一般是异常code枚举 data为空
     */
    public static DataResult getResult(BaseResponseCode responseCode){
        return new DataResult(responseCode);
    }

    /**
     * 自定义返回 入参一般是异常code枚举 data 可控
     */
    public static <T>DataResult getResult(BaseResponseCode responseCode, T data){

        return new DataResult(responseCode,data);
    }

    /**
     * 链式增加data数据的方法
     * @param key   数据的键
     * @param value 数据的值
     * @return  R对象
     */
    public DataResult addData(String key, Object value){
        this.dataMap.put(key, value);
        return this;
    }

}
